

def main():
    # 购物前：初始化账户、商品和购物车
    account = {'balance': float(input("请输入您的卡内余额：")), 'spent': []}
    goods = {
        "牛奶": {"price": 5.0, "stock": 10},
        "面包": {"price": 3.5, "stock": 8},
        "鸡蛋": {"price": 8.0, "stock": 15},
        "苹果": {"price": 10.5, "stock": 12},
        "香蕉": {"price": 6.0, "stock": 9}
    }
    cart = []
    display_goods(goods)
    while True:
        choice = input("\n请输入要购买的商品名称（输入Q或q退出）：").strip()
        if choice.upper() == 'Q':
            break
        if choice in goods:
            if goods[choice]["stock"] > 0:
                if account["balance"] >= goods[choice]["price"]:
                    cart.append(choice)
                    account["balance"] -= goods[choice]["price"]
                    account["spent"].append(goods[choice]["price"])
                    goods[choice]["stock"] -= 1
                    print(f"恭喜，购买成功！您的卡余额还剩{account['balance']:.2f}元")
                else:
                    print("穷鬼，钱不够！请到其他超市购买！")
            else:
                print("该商品已售罄！")
        else:
            print("没有这个商品，别瞎弄！")
    if cart:
        print("\n--- 购物小票 ---")
        for item in cart:
            print(f"{item}: {goods[item]['price']:.2f}元")
        print(f"总计: {sum(account['spent']):.2f}元")
        print(f"卡内余额: {account['balance']:.2f}元")
        print("感谢惠顾，欢迎下次光临！")
    else:
        print("您没有购买任何商品，欢迎下次光临！")
def display_goods(goods):
    print("商品列表：")
    for name, info in goods.items():
        print(f"{name}: {info['price']}元/件 (库存：{info['stock']})")


if __name__ == "__main__":
    main()
